<?php
/**
 * Created by PhpStorm.
 * User: guokunsheng
 * Date: 2018/12/3
 * Time: 17:23
 */

namespace app\pay\controller;

use \Alipay\aop\AopClient;
use \Alipay\aop\request\AlipayTradeQueryRequest as AppPayQueryRequest;
use think\Controller;

class Aliquery extends Controller
{
    /**
     * 支付宝订单查询
     * 文档地址：
     * https://docs.open.alipay.com/api_1/alipay.trade.query/
     * @param array $data
     * @param $account int 多商户切换
     * @return bool|mixed|\SimpleXMLElement
     */
    public function query($data = [], $account = 2)
    {
        $aliPayConfig = config('alipay');
        $config = $account ? $aliPayConfig['account' . $account] : $aliPayConfig['account' . $aliPayConfig['default']];

        $aop = new AopClient();
        $aop->gatewayUrl = $config['gatewayUrl'];
        $aop->appId = $config['app_id'];
        $aop->rsaPrivateKey = $config['merchant_private_key'];
        $aop->alipayrsaPublicKey = $config['alipay_public_key'];
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset = 'UTF-8';
        $aop->format = 'json';
        $request = new AppPayQueryRequest();

        $json = json_encode($data);
        $request->setBizContent($json);
        $result = $aop->execute($request);

        return $result;
    }
}
